/**
 * Created by Administrator on 14-3-27.
 */
var mybehave=Physics.behavior('player-behavior', function( parent ){
    return {
        init: function( options ){
            var self = this;
            parent.init.call(this, options);
            // the player will be passed in via the config options
            // so we need to store the player
            var player = self.player = options.player;
            self.gameover = false;
            //this.player.movePlayer=self.movePlayer;

            // events
            document.addEventListener('keydown', function( e ){
                if (self.gameover){
                    return;
                }
                switch ( e.keyCode ){
                    case 38: // up
                        self.movePlayer(true);
                        break;
                    case 40: // down
                        break;
                    case 37: // left
                        player.turn( -1 );
                        break;
                    case 39: // right
                        player.turn( 1 );
                        break;
                    case 90: // z
                        player.attack();
                        console.log("z");
                        break;
                }
                return false;
            });
            document.addEventListener('keyup', function( e ){
                if (self.gameover){
                    return;
                }
                switch ( e.keyCode ){
                    case 38: // up
                        self.movePlayer( false );
                        break;
                    case 40: // down
                        break;
                    case 37: // left
                        player.turn( 0 );
                        break;
                    case 39: // right
                        player.turn( 0 );
                        break;
                    case 90: // z
                        break;
                }
                return false;
            });
        },

        // this is automatically called by the world
        // when this behavior is added to the world
        connect: function( world ){

            // we want to subscribe to world events
            world.subscribe('collisions:detected', this.checkPlayerCollision, this);
            world.subscribe('integrate:positions', this.behave, this);
        },

        // this is automatically called by the world
        // when this behavior is removed from the world
        disconnect: function( world ){

            // we want to unsubscribe from world events
            world.unsubscribe('collisions:detected', this.checkPlayerCollision);
            world.unsubscribe('integrate:positions', this.behave);//积分就是每个时间轴都调用
        },

        // check to see if the player has collided
        checkPlayerCollision: function( data ){
            var col,collisions= data.collisions;
            for ( var i = 0, l = collisions.length; i < l; ++i ){
                col=collisions[i];
               // console.log(col.bodyB.fixed);可判断墙体

                map.rules(col);

            }
            return;
        },

        // toggle player motion
        movePlayer: function( active ){

            if ( active === false ){
                this.playerMove = false;
                return;
            }
            this.playerMove = true;
        },

        behave: function( data ){
            if(this.player.run)
            this.player.run();
            // activate thrusters if playerMove is true
            this.player.goahead( this.playerMove ? 10 : 0 );
        }
    };
});